package com.zr.planClass.controller;

import com.zr.planClass.model.*;
import com.zr.planClass.service.PlanClassService;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 天赐 on 2019/12/20.
 */
@RestController
@CrossOrigin//支持跨域
public class PlanClassController {

    @Autowired
    private PlanClassService planClassService;

    /*添加*/
    @PostMapping("planCategory/add")
    public ResultVo addPlanClass(@RequestBody @Valid PlanClassVo planClassVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }

        ResultVo resultVo = planClassService.add(planClassVo);
        return resultVo;
    }
    /*通过id获取值*/
    @GetMapping("queryById")
    public ResultVo queryById(Integer id){
        if (id == null){
            return ResultVoBuilder.error("id不能为空");
        }
        PlanClassAllVo planClassAllVo = planClassService.queryById(id);
        return ResultVoBuilder.success(planClassAllVo);
    }
    /*修改信息*/
    @PostMapping("planCategory/update")
    public ResultVo updatePlanClass(@RequestBody @Valid PlanClassVo planClassVo, BindingResult bindingResult){

        if (bindingResult.hasErrors()){
            return ResultVoBuilder.error(bindingResult.getFieldError().getDefaultMessage());
        }
        PlanClassAllVo planClassAllVo = planClassService.queryById(planClassVo.getId());
        if (!planClassAllVo.getStatus()){

            return planClassService.update(planClassVo);
        }
        return ResultVoBuilder.error("启用状态不能修改");
    }
    /*通过id获取信息*/
    @PostMapping("planCategory/updateStatus")
    @Transactional
    public ResultVo queryByIdStatus(@RequestBody @Valid PlanStutas planStutas){

        ResultVo resultVo = planClassService.updateStudas(planStutas);
        return ResultVoBuilder.success(resultVo);
    }


    /*分页,多条件*/
    @PostMapping("planCategory/queryPage")
    public ResultVo queryPage(@RequestBody PageSelectVo pageSelectVo){

        if(pageSelectVo.getStatusName().equals("0")){
            pageSelectVo.setStatus(false);
        }else if (pageSelectVo.getStatusName().equals("1")){
            pageSelectVo.setStatus(true);
        }
        ResultVo resultVo = planClassService.getPage(pageSelectVo);


        return resultVo;
    }

    @PostMapping("metadata/getPullDownList")
    public ResultVo xiala(){
        List<StatusVo> statusVoList = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel(0);
        statusVo.setValue("禁用");
        statusVoList.add(statusVo);

        StatusVo statusVo2 = new StatusVo();
        statusVo2.setLabel(1);
        statusVo2.setValue("启用");
        statusVoList.add(statusVo2);

        StatusVo statusVo3 = new StatusVo();
        statusVo3.setLabel(10);
        statusVo3.setValue("全部");
        statusVoList.add(statusVo3);
        return ResultVoBuilder.success(statusVoList);
    }

    @PostMapping("importPlanClass")
    public ResultVo importPlanClass(MultipartFile file) throws Exception{
        return planClassService.importPlanClass(file);
    }
}
